﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace AbetEvaluation
{
	public partial class AddOutcome : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				//editing an outcome.
				if (Page.Request.QueryString["id"] != null)
				{
					ViewState["outcomeid"] = Guid.Parse(Page.Request.QueryString["id"]);

					//load outcome.
					dvOutcome.ChangeMode(DetailsViewMode.ReadOnly);
					dvOutcome.DataBind();

					//load questions.
					pnlQuestions.Visible = true;
					pnlRubrics.Visible = true;
				}

				//creating an outcome.
				else
				{
					ViewState["outcomeid"] = new Guid(); //blank guid. all zeros.
				}
			}
		}

		protected void DataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters.Add("OutcomeID", (Guid)ViewState["outcomeid"]);
		}

		protected void dvQuestions_ItemInserting(object sender, DetailsViewInsertEventArgs e)
		{
			bool answer = (dvQuestions.FindControl("radioYes") as RadioButton).Checked;
			e.Values.Add("Answer", answer);
			e.Values.Add("OutcomeID", (Guid)ViewState["outcomeid"]);
		}

		protected void dvQuestions_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
		{
			gvQuestions.DataBind();
		}

		protected void dvRubric_ItemInserting(object sender, DetailsViewInsertEventArgs e)
		{
			e.Values.Add("OutcomeID", (Guid)ViewState["outcomeid"]);
		}

		protected void dvRubric_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
		{
			gvRubric.DataBind();
		}

		protected void btnCreate_Click(object sender, EventArgs e)
		{
			lblDebug.Text += "<br>Create Clicked.";
		}

		protected void btnClear_Click(object sender, EventArgs e)
		{
			lblDebug.Text += "<br>Clear Clicked.";
		}

		protected void dvOutcome_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
		{
			dvOutcome.ChangeMode(DetailsViewMode.ReadOnly);
			dvOutcome.DataBind();

			pnlQuestions.Visible = true;
			pnlRubrics.Visible = true;
		}

		protected void dvOutcome_ItemInserting(object sender, DetailsViewInsertEventArgs e)
		{
			e.Values.Add("ID", (Guid)ViewState["outcomeid"]);
			e.Values.Add("CreatedBy", User.Identity.Name);
		}

		protected void btnNewOutcome_Click(object sender, EventArgs e)
		{
			dvOutcome.ChangeMode(DetailsViewMode.Insert);
			ViewState["outcomeid"] = Guid.NewGuid();

			pnlQuestions.Visible = false;
			pnlRubrics.Visible = false;
		}

		protected void dvOutcome_ModeChanging(object sender, DetailsViewModeEventArgs e)
		{
			switch (e.NewMode)
			{
				case DetailsViewMode.Insert:
					ViewState["outcomeid"] = Guid.NewGuid();
					pnlQuestions.Visible = false;
					pnlRubrics.Visible = false;
					break;
				case DetailsViewMode.ReadOnly:
					break;
			}
		}

		//protected void gvQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
		//{
		//    if (e.Row.RowType == DataControlRowType.DataRow)
		//    {
		//        if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
		//        {
		//            if ((bool)DataBinder.Eval(e.Row.DataItem, "Answer") == true)
		//                (e.Row.FindControl("radioYes") as RadioButton).Checked = true;
		//            else
		//                (e.Row.FindControl("radioNo") as RadioButton).Checked = true;
		//        }
		//        else if ((e.Row.RowState & DataControlRowState.Normal) == DataControlRowState.Normal)
		//        {
		//            (e.Row.FindControl("lblAnswer") as Label).Text = (bool)DataBinder.Eval(e.Row.DataItem, "Answer") == true ? "Yes" : "No";
		//        }
		//    }
		//}
	}
}